package com.emokili.ewa.frame.controller.v1;

import com.emokili.ewa.common.utils.R;
import com.emokili.ewa.frame.utils.QRCodeUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;

@RestController
@RequestMapping("v1/frame")
public class QrCodeController {

    @RequestMapping("/qrcode")
    @ResponseBody
    public R code(String serialNumber) throws Exception {
        // 存放在二维码中的内容
        String text = "我是小铭";
        // 生成的二维码的路径及名称
        String destPath = "E:/test/jam.jpg";
        //生成二维码
        BufferedImage image = QRCodeUtil.encode(text, null, destPath, true);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg",bos);
        byte[] bytes = bos.toByteArray();
        String base64String = Base64.getEncoder().encodeToString(bytes);
        return R.ok().setData("data:image/jpg;base64,"+base64String);
    }



    public static void main(String[] args)throws Exception {
        // 存放在二维码中的内容
        String text = "我是小铭";
        // 嵌入二维码的图片路径
        String imgPath = "G:/qrCode/dog.jpg";
        // 生成的二维码的路径及名称
        String destPath = "E:/test/jam.jpg";
        //生成二维码
        BufferedImage image = QRCodeUtil.encode(text, null, destPath, true);
        // 解析二维码
         String str = QRCodeUtil.decode(destPath);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(image,"jpg",bos);
        byte[] bytes = bos.toByteArray();
        String base64String = Base64.getEncoder().encodeToString(bytes);

        // 打印出解析出的内容
        System.out.println(str);
    }
}
